//
//  ArcLineView.m
//  KEREN
//
//  Created by MW on 2023/4/6.
//

#import "ArcLineView.h"

@implementation ArcLineView
-(instancetype)initWithFrame:(CGRect)frame starScore:(float)starScore{
    self = [super initWithFrame:frame];
    if(self){
        _starScore = starScore;
        [self addUI];
    }
    return self;
}
- (void)drawRect:(CGRect)rect {
    self.backgroundColor = [UIColor whiteColor];
    //设置圆弧的半径
    CGFloat radius =  rect.size.width/2;
    //设置圆弧的圆心
    CGPoint center = CGPointMake(radius, radius);
    
    //背景线条
    CGFloat startAngleBag = DEGREES_TO_RADIANS(180);
    CGFloat endAngleBag = DEGREES_TO_RADIANS(180) + DEGREES_TO_RADIANS(180);
    UIBezierPath *pathBag = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 6 startAngle:startAngleBag endAngle:endAngleBag clockwise:YES];
    pathBag.lineWidth = 12;
    pathBag.lineCapStyle = kCGLineCapRound;
    [Color_C3C5C9 set];
    [pathBag stroke];
    
    //显示的进度条
    CGFloat startAngle = DEGREES_TO_RADIANS(180);
    CGFloat endAngle = DEGREES_TO_RADIANS(180) + DEGREES_TO_RADIANS(180)*_starScore;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 6 startAngle:startAngle endAngle:endAngle clockwise:YES];
    path.lineWidth = 12;
    path.lineCapStyle = kCGLineCapRound;
    [Color_7ACC00 set];
    [path stroke];
 
}

- (void)addUI{
    UILabel *label = [UILabel setLblText:@"今日耗电" font:Font(16) withColor:Color_86898E inSubView:self];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-12);
        make.centerX.equalTo(self);
    }];
    
    UILabel *countLbl = [UILabel setLblText:[NSString stringWithFormat:@"%d%@",(int)(_starScore * 100),@"%"] font:Font(60) withColor:Black_Color inSubView:self];
    [countLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(label.mas_top).offset(0);
        make.centerX.equalTo(self);
    }];
    NSAttributedString *tAttString = [NSAttributedString getAttributeWith:@[@"%"] string:[NSString stringWithFormat:@"%d%@",(int)(_starScore * 100),@"%"] Font:Font(60) Color:Black_Color attributeFont:Font(20) attributeColor:Black_Color];
    countLbl.attributedText = tAttString;

}

@end
